//
//  HomeListTableViewCell.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "HomeListTableViewCell.h"
#import <CoreLocation/CLAvailability.h>
#import <CoreLocation/CoreLocation.h>

@interface HomeListTableViewCell ()

@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UIButton *infoBtn;

@end

@implementation HomeListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.layer.masksToBounds = YES;
        _imgView.backgroundColor = getColor(@"f1f2f6");
        
        _infoLabel = [UILabel new];
        _infoLabel.numberOfLines = 0;
        _infoLabel.font = DEF_FontSize_13;
        
        _infoBtn = [UIButton new];
        [_infoBtn setTitle:@"详情" forState:UIControlStateNormal];
        [_infoBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _infoBtn.titleLabel.font = DEF_FontSize_12;
        
        [self.contentView addSubview:_imgView];
        [self.contentView addSubview:_infoBtn];
        [self.contentView addSubview:_infoLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _imgView.frame = CGRectMake(15, 0, self.frame.size.height-15, self.frame.size.height-15);
    _imgView.center = CGPointMake(_imgView.center.x, self.frame.size.height/2.);
    
    _infoLabel.frame = CGRectMake(CGRectGetMaxX(_imgView.frame)+10, 0, SCREEN_WIDTH - CGRectGetMaxX(_imgView.frame) - 60, self.frame.size.height);
    _infoBtn.frame = CGRectMake(SCREEN_WIDTH-50, 0, 40, 40);
    _infoBtn.center = CGPointMake(_infoBtn.center.x, self.frame.size.height/2.);
}

- (void)setDataDictionary:(NSDictionary *)dataDictionary{
    _dataDictionary = dataDictionary;
    [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:dataDictionary[@"picture"]]];
    
    NSString *title = dataDictionary[@"s_name"];
    NSString *info = [NSString stringWithFormat:@"商品描述:%@",dataDictionary[@"describe"]];
    NSString *temp = [NSString stringWithFormat:@"%@\n%@\n",title,info];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:temp];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"545454") range:[temp rangeOfString:info]];
    
    NSTextAttachment *attText = [NSTextAttachment new];
    attText.image = [UIImage imageNamed:@"location"];
    attText.bounds = CGRectMake(0, 0, 8, 10);
    [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:attText]];
    double address = [self distanceBetweenOrderBy:[[UserModel sharedInstanced].latitude doubleValue ]:[dataDictionary[@"s_latitude"] doubleValue] :[[UserModel sharedInstanced].longitude doubleValue]:[dataDictionary[@"s_longitude"] doubleValue]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" 当前距离%.1fKM",address] attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];//调整行间距
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [temp length])];
    
    _infoLabel.attributedText = attStr;

}

-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance / 1000.0;
    
}

@end
